#include "calendarShowWindow.h"
#include "ui_calendarShowWindow.h"

CalendarShowWindow::CalendarShowWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::CalendarShowWindow)
{
    ui->setupUi(this);
//    _icon = QIcon(":/images/icon.png");
//    setWindowIcon(_icon);
    ui_editItemDialog = new editItemDialog;
    slotClickedDate(QDate::currentDate());//initialize selected date with currentDate

    connect(ui->calendarWidget,SIGNAL(clicked(QDate)),this,SLOT(slotClickedDate(QDate)));
    connect(ui->calendarWidget,SIGNAL(activated(QDate)),this,SLOT(slotAddNoteItem(QDate)));//double click
}

CalendarShowWindow::~CalendarShowWindow()
{
    delete ui;
    SAFE_DELETE_POINTER(ui_editItemDialog);
}

NoteItemMap * CalendarShowWindow::getItemMap()
{
    return ui_editItemDialog->getItemMap();
}

void CalendarShowWindow::reloadData(QString fileName)
{
    ui_editItemDialog->readFromFile(fileName);
    //ui_editItemDialog->synchronismData();
}

void CalendarShowWindow::backupData(QString fileName)
{
    ui_editItemDialog->saveToFile(fileName);
}

void CalendarShowWindow::update()
{
    //ui->calendarWidget->setWindowIcon(_icon);

    slotClickedDate(_selectedData);//
}

void CalendarShowWindow::slotClickedDate(QDate _date)
{
    ui->listWidget_Notes->clear();
    _pItemOneDay = ui_editItemDialog->getItemOneday(_date);

    QMap<QString,NoteItem>::const_iterator iter = _pItemOneDay->constBegin();
    for(; iter!=_pItemOneDay->constEnd();++iter)
        ui->listWidget_Notes->addItem(((NoteItem)iter.value()).getTitle());

}

void CalendarShowWindow::slotAddNoteItem(QDate _date)
{
    //ui->dateEdit->setDate(_date);

    //ui->textBrowser->setText(_itemMap.getItemOneDay(_date).getItem(0).getTitle());
    _selectedData = _date;//conserve selected date,after edit Note should call update() to update this UI

    ui_editItemDialog->updateUI();
    ui_editItemDialog->setDate(_date);
    ui_editItemDialog->setItemOneDay(_date);
    ui_editItemDialog->show();
}


